<template>
  <div class="pages sysdoor">
    <div class="pageLevel">
      <div>
        <div class="tableSearch">
          <el-form label-width="110px" class="demo-ruleForm" :model="ruleForm" :rules="rulesza">
            <el-form-item label="可抹零人员" prop="linkName">
              <el-input placeholder="请选择用户" disabled v-model="ruleForm.linkName">
                <el-button type="primary" @click="selectUserId(0)" slot="append">选择</el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="businessScope" label="不限制抹零金额" style="margin-left:20px">
              <el-checkbox
                true-label="0"
                false-label="1"
                name="businessScope"
                v-model="ruleForm.mlAccess"
                @change="Checkbox"
              ></el-checkbox>
            </el-form-item>
            <el-form-item label="抹零金额" prop="mlMoney">
              <sd-input :val.sync="ruleForm.mlMoney" :disableds="ruleForm.mlAcce"></sd-input>
            </el-form-item>
            <el-form-item label label-width="15px">
              <el-button type="primary" size="mini" class="Button" @click="addTopAccessUser('0')">添加</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="fonts">
          <el-table border :data="topSelect" size="medium" style="width: 400px">
            <el-table-column type="index" align="center" label="序号"></el-table-column>
            <el-table-column prop="linkName" align="center" label="用户姓名"></el-table-column>
            <el-table-column prop="mlMoney" align="center" label="抹零金额"></el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button @click.native="deleteAccessUser(scope.row.id)" type="text">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>

      <div>
        <div class="tableSearch newTableSearch">
          <b class="colorB">退货修改权配置</b>
          <el-form label-width="150px" class="demo-ruleForm">
            <el-form-item label="退货金额可修改人员">
              <el-input placeholder="请选择用户" disabled v-model="linkName">
                <el-button type="primary" @click="selectUserId(1)" slot="append">选择</el-button>
              </el-input>
            </el-form-item>
            <el-form-item label label-width="15px">
              <el-button type="primary" size="mini" class="Button" @click="addTopAccessUser('1')">添加</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="fonts">
          <el-table :data="bottomSelect" border size="medium" style="width: 400px">
            <el-table-column type="index" align="center" label="序号"></el-table-column>
            <el-table-column prop="linkName" align="center" label="用户姓名"></el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button @click.native="deleteAccessUser(scope.row.id)" type="text">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div>
        <!-- 用户选择弹出框 -->
        <el-dialog
          title="用户选择"
          @opened="dialogOpen"
          :visible.sync="dialogTableVisible"
          class="addshopname"
          :closeOnClickModal="false"
          :closeOnPressEscape="false"
          width="50%"
          v-model="ruleForm.linkName"
        >
          <i class="el-icon-close icon" @click="dialogTableVisible=false"></i>
          <div class="operationBox tableSearch">
            <el-button size="mini" type="primary" @click="save">选择</el-button>
            <el-button size="mini" @click="dialogTableVisible=false">取消</el-button>
          </div>
          <div class="tableSearch">
            <el-form label-width="100px" class="demo-ruleForm">
              <el-form-item label label-width="15px">
                <!-- @keyup.enter.native="searchSelectUser" -->
                <el-input
                  class="input"
                  size="small"
                  v-model="selectData.userkey"
                  placeholder="请输入用户名"
                ></el-input>
              </el-form-item>
              <el-form-item label label-width="15px">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="searchSelectUser"
                  class="Button"
                >查询</el-button>
              </el-form-item>
            </el-form>
          </div>
          <el-table
            :data="tableData"
            style="width: 100%"
            stripe
            border
            ref="multipleTable"
            tooltip-effect="dark"
            highlight-current-row
            @selection-change="changeUser"
            :row-key="getRowKeys"
          >
            <!-- :row-key="getRowKeys" -->
            <el-table-column type="selection" width="55" :reserve-selection="true"></el-table-column>
            <el-table-column type="index" align="center" label="序号"></el-table-column>
            <el-table-column prop="linkName" label="用户名称" align="center"></el-table-column>
          </el-table>
          <page-unit :pageData="pageData" :func="selectUser"></page-unit>
        </el-dialog>
      </div>
      <div style="margin-top:20px">
        <b class="colorB">班次配置</b>
        <div class="scheduling_box">
          <el-form class="demo-ruleForm form-row">
            <el-form-item label="是否开启排班" style="width:400px">
              <el-radio v-model="radio" label="1" @change="openSwitch(1)">开启</el-radio>
              <el-radio v-model="radio" label="0" @change="openSwitch(0)">关闭</el-radio>
            </el-form-item>
          </el-form>
          <div v-for="(item,index) in findList" :key="index" class="findlist">
            <el-form label-width="20px">
              <el-form-item label>
                <el-input
                  disabled
                  @keyup.enter.native="searchSelectUser"
                  style="width:150px;"
                  size="small"
                  v-model="item.scheduleName"
                  placeholder="请输入用户名"
                ></el-input>
              </el-form-item>
              <el-time-select
                placeholder="起始时间"
                v-model="item.scheduleStartTime"
                disabled
                style="margin-left:20px"
              ></el-time-select>
              <el-time-select
                placeholder="结束时间"
                v-model="item.scheduleEndTime"
                disabled
                style="margin-left:20px"
              ></el-time-select>
              <div class="button_s">
                <el-button type="primary" size="mini" @click.native="deleteAccessUsers(item.id)">删除</el-button>
                <!-- <el-button type="primary"  size="mini">主要按钮</el-button> -->
              </div>
              <!-- </el-form-item> -->
            </el-form>
          </div>
        </div>
        <el-dialog
          title="添加班次"
          @opened="dialogOpen"
          :visible.sync="dialogVisibles"
          class="addshopname"
          :closeOnClickModal="false"
          :closeOnPressEscape="false"
          width="60%"
        >
          <i class="el-icon-close icon" @click="dialogVisibles_s"></i>
          <el-form label-width="100px">
            <el-form-item label="班次名:">
              <el-input
                @keyup.enter.native="searchSelectUser"
                style="width:150px;"
                size="small"
                v-model="selectData.scheduleName"
                placeholder="请输入班次"
              ></el-input>
            </el-form-item>
            <el-form-item label="时间段:">
              <el-time-picker
                is-range
                v-model="dates"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                value-format="timestamp"
              ></el-time-picker>
            </el-form-item>
            <el-form-item label>
              <el-button type="primary" @click="addScheduling" class="seve">保存</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
        <div class="primary">
          <el-button type="primary" @click="dialogVisibles = true" class="primaryss">添加班次</el-button>
        </div>
      </div>
      <!-- <div>
        <div style="width:500px;  margin:50px 20px 50px 30px;" class="foot">
          <el-button type="primary" class="Determine" @click="preservation('ruleForms')">保存</el-button>
        </div>
      </div>-->
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import util from "@/util";
import sdInput from "@/components/SdInput";
import pageUnit from "@/components/Page";
import {
  findUserList,
  selectAccessList,
  addAccessUser,
  deleteAccessUser,
  findScheduleList,
  saveOrUpdateSchedule,
  deleteSchedule,
  scheduleSwitch
} from "@/api";
export default {
  components: { sdInput, pageUnit },
  computed: {
    ...mapState(["sub", "merchantId", "power"])
  },
  data() {
    return {
      getRowKeys(row) {
        return row.id;
      },
      pageData: {
        pageSize: 20,
        currentPage: 1,
        totalItems: 0,
        pageSizeArr: [5, 10, 20]
      },
      radio: "1",
      dialogVisibles: false,
      dialogVisible: false,
      dateTime: [],
      dates: new Date(),
      topSelect: [],
      bottomSelect: [],
      sListData: [],
      currentPage: 1,
      pageSize: 5,
      totalItems: 0,
      checked: false,

      mlMoney: "",
      ruleForm: {
        userIds: "",
        linkName: "",
        orgId: "",
        accessType: "",
        mlMoney: "",
        mlAccess: "1",
        mlAcce: false
      },
      rulesza: {},
      rules: {},
      linkName: "",
      paibanName: "",
      findList: [],
      finListscheduleName: "",
      selectData: {
        userkey: "",
        scheduleName: ""
      },

      currentRow: null,
      tableData: [],
      allData: [],
      userArray: [],
      dialogTableVisible: false,
      selectObj: [],
      userIds: [], // 用户id
      ruleForm: {},
      value: "",
      addLabel: 0,

      buttonLable: true,
      buttonImg: true,
      buttonImg1: true,
      buttonImg2: true,
      buttonImg3: true,
      buttonImgList: 0,

      userIds: [] // 用户id
    };
  },
  mounted() {
    this.findScheduleList();
    this.repertory();
    this.openSwitch();
    // this.storeDetails();
  },
  methods: {
    Checkbox(vel) {
      console.log(vel);
      if (vel == 0) {
        this.ruleForm.mlAcce = true;
        this.ruleForm.mlMoney = 0;
      } else {
        this.ruleForm.mlAcce = false;
      }
    },
    //配置权限
    setRole(key) {
      return util.selectTitle(this.power, key);
    },
    dialogVisibles_s() {
      this.dialogVisibles = false;
      this.selectData.scheduleName = "";
      this.dates = "";
      // this.findScheduleList();
    },
    findScheduleList() {
      let idData = {
        orgId: this.merchantId
      };
      findScheduleList(idData).then(res => {
        this.findList = res.data.list;
      });
    },
    addScheduling() {
      if (this.selectData.scheduleName == "") {
        this.$message({
          showClose: true,
          message: "请输入班次名称",
          type: "error"
        });
        return false;
      }
      this.dialogVisibles = false;
      let scheduleStartTime = util.fortime(this.dates[0]);
      let scheduleEndTime = util.fortime(this.dates[1]);
      let addData = {
        data: JSON.stringify([
          {
            scheduleName: this.selectData.scheduleName,
            scheduleStartTime: scheduleStartTime,
            scheduleEndTime: scheduleEndTime
          }
        ]),
        orgId: this.merchantId
      };
      saveOrUpdateSchedule(addData).then(res => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg
          });
        }
        this.selectData.scheduleName = "";
        this.dates = "";
        this.findScheduleList();
      });
    },
    searchSelectUser() {
      this.selectUser(this.pageData.pageSize,1);
    },
    //用户列表
    selectUser(pageSize, pageNum) {
      findUserList({
        merchantId: this.merchantId,
        pageNum: pageNum,
        pageSize: pageSize,
        findUserName:this.selectData.userkey
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.pageData.totalItems = res.data.pageTotal;
          res.data.userList.map(i => {
            i.show = false;
          });
          this.pageData.currentPage=pageNum
          this.tableData = res.data.userList;
        }
      });
    },
    selectUserId(type) {
      this.selectData.userkey = "";
      this.selectUser(this.pageData.pageSize,1);
      this.dialogTableVisible = true;
      this.userType = type;
    },
    dialogOpen() {
      this.selectObj = [];
    },
    //选择用户
    changeUser(val) {
      this.selectObj = val;
    },
    save() {
      this.userArray = [];
      let arr = [];
      let ids = [];
      console.log(this.selectObj);
      if (this.selectObj.length == 0) {
        this.$message({
          showClose: true,
          message: "请选择用户！",
          type: "error"
        });
      } else {
        this.selectObj.map(i => {
          arr.push(i.linkName), ids.push(i.userId);
        });
        this.userIds = ids.toString();
        if (this.userType === 0) {
          this.ruleForm.linkName = arr.toString();
          //  this.ruleForm.linkName = this.selectObj.linkName
        } else if (this.userType === 1) {
          this.linkName = arr.toString();
        } else {
          this.paibanName = arr.toString();
        }
        // this.userIds = this.selectObj.id.toString();
        this.dialogTableVisible = false;
      }
    },
    deleteAccessUser(id) {
      this.$confirm("确定要删除吗?", "提示", {
        center: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false,
        closeOnPressEscape: false
      }).then(() => {
        deleteAccessUser({ id })
          .then(res => {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.repertory();
          })
          .catch(res => {
            this.$message({
              type: "error",
              message: res.msg
            });
          });
      });
    },
    //删除班次
    deleteAccessUsers(id) {
      this.$confirm("确定要删除吗?", "提示", {
        center: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false,
        closeOnPressEscape: false
      }).then(() => {
        deleteSchedule({ id })
          .then(res => {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.findScheduleList();
          })
          .catch(res => {
            this.$message({
              type: "error",
              message: res.msg
            });
          });
      });
    },
    addTopAccessUser(type) {
      // console.log(this.linkName);
      if (
        (type == 0 &&
          (this.ruleForm.linkName == "" ||
            this.ruleForm.linkName === undefined)) ||
        (type == 1 && this.linkName === "") ||
        (type == 2 && this.paibanName === "")
      ) {
        this.$message({
          showClose: true,
          message: "请选择用户",
          type: "error"
        });
      } else {
        let postdata = {
          userId: this.userIds,
          orgId: this.merchantId,
          accessType: type,
          mlMoney: this.ruleForm.mlMoney,
          mlAccess: this.ruleForm.mlAccess
        };
        addAccessUser(postdata).then(res => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: res.msg
            });
          }
          this.ruleForm.linkName = "";
          this.linkName = "";
          this.paibanName = "";
          this.ruleForm.mlMoney = "";
          this.repertory();
        });
      }
    },
    repertory() {
      let data = {
        shopId: this.merchantId
      };
      selectAccessList(data).then(res => {
        this.bottomSelect = res.data.list;
        this.topSelect = res.data.mlList;
        this.sListData = res.data.sList;
      });
    },
    //班次开关
    openSwitch(isSchedul) {
      let datas = {};
      if (isSchedul == undefined) {
        // postdata.shopId=this.merchantId
        datas = {
          shopId: this.merchantId
        };
      } else {
        datas = {
          shopId: this.merchantId,
          isSchedul: isSchedul
        };
      }
      scheduleSwitch(datas).then(res => {
        if (res.code == 200) {
          if (isSchedul == undefined) {
            this.radio = res.data.isSchedul.toString();
          }
        } else {
          this.$message({
            type: "error",
            message: res.msg
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.colorB {
  color: #666;
}
h5 {
  height: 0px;
}
.newTableSearch {
  margin-top: 20px;
}
.addshopname .userinputs .el-input {
  width: 100px;
}
.addshopname .userinputs .el-pagination__editor.el-input .el-input__inner {
  width: 50px;
}
.tableSearch .el-form-item__content {
  margin-left: 20px !important;
}
.scheduling_box {
  flex-wrap: wrap;
  /* flex-direction: column; */
  display: flex;
  width: 100%;
  align-items: center;
  margin-top: 20px;
}
/* .scheduling {
  position: relative;
  width: 600px;
  margin-top: 10px;
  background-color: rgba(242, 242, 242, 1);
  height: 120px;
  margin-left: 100px;
} */
.del {
  position: absolute;
  top: 0;
  right: 0;
}
.primary {
  margin-top: 20px;
  margin-left: 100px;
  height: 150px;
  line-height: 150px;
}

.seve {
  margin-top: 6px;
}
.findlist {
  position: relative;
  margin-left: 10px;
  margin-top: 10px;
  width: 520px;
  background-color: rgba(242, 242, 242, 1);
}
.button_s {
  position: absolute;
  right: 12px;
  top: 6px;
  margin-left: 10px;
}
</style>
<style scoped>
.addshopname .userinputs .el-input {
  width: 100px;
}
.addshopname .userinputs .el-pagination__editor.el-input .el-input__inner {
  width: 50px;
}
.tableSearch .el-form-item__content {
  margin-left: 20px !important;
}
.jichu {
  width: 100%;
  color: #444;
  font-size: 14px;
  border-bottom: 1px dashed #ccc;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  height: 178px;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style scoped>
* {
  box-sizing: border-box;
}
.bottom {
  width: 100%;
  margin-top: 10px;
}
.replace {
  float: left;
  width: 50%;
  height: 20px;
  text-align: center;
  line-height: 20px;
  position: relative;
  overflow: hidden;
}
.replace div {
  position: absolute;
  width: 100%;
  height: 20px;
  text-align: center;
  line-height: 20px;
}
.replace input {
  position: absolute;
  opacity: 0;
  width: 100%;
  left: 0;
}
.delete {
  float: left;
  width: 50%;
  height: 20px;
  text-align: center;
  line-height: 20px;
}
.top {
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.center {
  height: 30px;
  line-height: 1.3;
}
.form-row {
  width: 100%;
}
</style>
